<?php

namespace App\Admin\Controllers;

use App\Models\TaskSubmit;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class TaskSubmitController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('任务提交内容')
            ->description('任务提交内容')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new TaskSubmit);

        $grid->id('Id');
        $grid->task()->name('任务名称');
        $grid->step('第几步');
        $grid->type('类型')->display(function($value){
            return ($value == 1)?'文字':'图片';
        });
        $grid->title('标题');
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');
        $grid->disableRowSelector();
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->equal('task_id','选择任务')->select('/admin/task/get_task_list');
            $filter->where(function ($query) {
                $query->whereHas('task', function ($query) {
                    $query->where('name', 'like', "%{$this->input}%");
                });
            }, '任务名称');
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(TaskSubmit::findOrFail($id));

        $show->id('Id');
        $show->task_id('Task id');
        $show->step('Step');
        $show->type('Type');
        $show->title('Title');
        $show->content('Content');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new TaskSubmit);

        $form->select('task_id', '任务名称')->options('/admin/task/get_task_list');
        $form->number('step', '第几步');
        $form->radio('type', '类型')->options([1 => '文字', 2 => '截图']);
        $form->text('title', '标题');

        return $form;
    }
}
